package cn.cy.dao.orclimpl;

import cn.cy.dao.BaseDao;
import cn.cy.dao.BizClaimVoucherDao;
import cn.cy.pojo.BizClaimVoucher;
import cn.cy.pojo.ClaimVoicherCondition;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Repository;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
@Repository("claimVoucherDao")
public class BizClaimVoucherDaoImpl extends BaseDao<BizClaimVoucher> implements BizClaimVoucherDao {
    public BizClaimVoucherDaoImpl(){}
    @Autowired
    public BizClaimVoucherDaoImpl(@Qualifier("sessionFactory") SessionFactory sessionFactory){
        setSessionFactory(sessionFactory);
    }
    @Override
    public List<BizClaimVoucher> findAll() {
        return this.getHibernateTemplate().find("from BizClaimVoucher");
    }

    @Override
    public List<String> findStatusAll() {
        String hql = "SELECT status FROM BizClaimVoucher GROUP BY status ";
        return this.getHibernateTemplate().find(hql);
    }

    @Override
    public BizClaimVoucher findById(Long id) {
        String hql = "from BizClaimVoucher where id="+id;
        return (BizClaimVoucher)getHibernateTemplate().find(hql).get(0);
    }
    @Override
    public Long count(){
        String hql = "select COUNT(id) from BizClaimVoucher";
        return (Long)this.getHibernateTemplate().find(hql).get(0);
    }

    @Override
    public List<BizClaimVoucher> findSatausAnndDate(final String hql, final ClaimVoicherCondition claimVoicherCondition) {
        return this.getHibernateTemplate().execute(new HibernateCallback<List<BizClaimVoucher>>() {
            @Override
            public List<BizClaimVoucher> doInHibernate(Session session) throws HibernateException, SQLException {
                Query query = session.createQuery(hql).setProperties(claimVoicherCondition);
                return query.setFirstResult((claimVoicherCondition.getCurrPageNo() - 1) * claimVoicherCondition.getPageSize())
                        .setMaxResults(claimVoicherCondition.getPageSize()).list();
            }
        });
        }

}
